package com.amigo.online.message.controller;

import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.base.security.LocalUser;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.core.security.IgnoreSecurity;
import com.amigo.online.common.param.dto.group.AddGroupMemberDto;
import com.amigo.online.common.param.dto.group.NewGroupDto;
import com.amigo.online.message.exception.GroupException;
import com.amigo.online.message.mybatisbean.GroupTbl;
import com.amigo.online.message.service.GroupService;
import com.amigo.online.common.core.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api("群聊")
@RequestMapping("/group")
@RestController
public class GroupController {

    @Autowired
    GroupService groupService;

    /**
     * 创建普通群聊
     * @param groupDto
     * @return
     */
    @ApiOperation(value = "创建普通群聊" ,  notes="创建普通群聊")
    @PostMapping(value = "/createTopic")
    public Result foundTopic(@RequestBody NewGroupDto groupDto){
        Long uId = LocalUser.getUser().getUser().getId();
        return Result.success(groupService.foundTopic(groupDto, uId));
    }



    @ApiOperation(value = "邀请用户进群" ,  notes="邀请用户进群")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "自己的群ID",dataType = "Long"),
            @ApiImplicitParam(name = "accounts", value = "用户ID",dataType = "String"),
    })
    @PostMapping(value = "/invitation")
    public Result invitation(@RequestBody  AddGroupMemberDto addGroupMemberDto){
        return groupService.invitation(addGroupMemberDto.getGroupId(),addGroupMemberDto.getMemberIds());
    }

    /**
     *退群
     * @param groupId
     * @return
     */
    @ApiOperation(value = "退群" ,  notes="退群")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token",dataType = "String"),
            @ApiImplicitParam(name = "topicId", value = "im群ID",dataType = "Long"),
            @ApiImplicitParam(name = "groupId", value = "自己的群ID",dataType = "Long"),
    })
    @PostMapping(value = "/quitGroup")
    public Result quitGroup(Long groupId){
        return groupService.quitGroup(groupId);
    }

    /**
     * 解散群
     * @param userToken
     * @param topicId
     * @param groupId
     * @return
     */
    @ApiOperation(value = "解散群" ,  notes="解散群")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token",dataType = "String"),
            @ApiImplicitParam(name = "topicId", value = "im群ID",dataType = "Long"),
            @ApiImplicitParam(name = "groupId", value = "自己的群ID",dataType = "Long"),
    })
    @PostMapping(value = "/dissolutionGroup")
    public Result dissolutionGroup(String userToken, Long topicId, long groupId){
        return  groupService.dissolutionGroup(userToken, topicId, groupId);
    }


    @ApiOperation(value = "查询所属群信息" ,  notes="查询所属群信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户token",dataType = "String",paramType = "body")
    })
    @PostMapping("/selectOwnGroup")
    public Result selectOwnGroup(String userId){
        return  groupService.selectOwnGroup(userId);
    }

    @ApiOperation(value = "查询指定群信息" ,  notes="查询指定群信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "groupId",dataType = "String",paramType = "body"),
    })
    @GetMapping("/selectGroupByTopicId")
    public Result selectGroupByTopicId(Long groupId){
        if(groupId == null){
            throw new GroupException(StatusCode.GROUPID_ISNULL);//群ID不能为空
        }
        return  groupService.selectGroupByTopicId(groupId);
    }

    @ApiOperation(value = "群主踢用户退群" ,  notes="群主踢用户退群")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "群主token",dataType = "String",paramType = "body"),
            @ApiImplicitParam(name = "accounts", value = "用户ID",paramType = "body")
    })
    @PostMapping("/kickQuitGroup")
    @IgnoreSecurity
    public Result kickQuitGroup(String userToken,String topicId, long userId, long groupId){
        return  groupService.kickQuitGroup(userToken,topicId,userId,groupId);
    }
    /**
     * 更新群信息
     */
    public Result updeteGroupInfo(@RequestBody Map map){

        return Result.error();
    }

    }
